﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCSLibrary;
using Utils.DatabaseUtils;
using Utils;
using Utils.DC.Web.UI;
using Telerik.Web.UI;

public partial class Admin_Module_quan_tri_nhom_quyen : System.Web.UI.UserControl
{
    private RoleUI objRole = new RoleUI();

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindRole();
        }
    }

    private void BindRole()
    {
        DataSet ds = objRole.GetAll();
        if (ds != null && ds.Tables.Count > 0)
        {
            grd.DataSource = ds.Tables[0];
            grd.DataBind();
        }
    }

    protected void btnSetRole_Click(object sender, EventArgs e)
    {
        if (lblRoleID.Text.Trim() == "")
        {
            try
            {
                int result = objRole.Add(txtRoleName.Text.Trim(),null,txtRoleDescription.Text.Trim(),1,GetCheckedPageList());
                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                //lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
                lblMessage.Text = ex.ToString();
            }
        }
        else
        {
            try
            {

                int result = objRole.Update(Convert.ToInt32(lblRoleID.Text.Trim()), txtRoleName.Text.Trim(), null, txtRoleDescription.Text.Trim(), 1, GetCheckedPageList());

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }

    protected void btnEditRole_Click(object sender, EventArgs e)
    {
        LinkButton link = (LinkButton)sender;
        int roleID = Convert.ToInt32(link.CommandArgument);
        DataSet dsRole = objRole.GetByID(roleID);
        if (TableLib.HasData(dsRole))
        {
            panel1.Visible = true;
            txtRoleName.Text = dsRole.Tables[0].Rows[0]["RoleName"].ToString().Trim();
            txtRoleDescription.Text = dsRole.Tables[0].Rows[0]["Description"].ToString().Trim();
            lblRoleID.Text = dsRole.Tables[0].Rows[0]["RoleID"].ToString().Trim();
            BindAdminPage();
            LoadPageInRole(dsRole.Tables[1]);
            btnSetRole.Text = "Cập nhật";
        }

    }

    protected void btnDeleteRole_Click(object sender, EventArgs e)
    {
        LinkButton link = (LinkButton)sender;
        int roleID = Convert.ToInt32(link.CommandArgument);
        try
        {
            int result = objRole.DeleteByID(roleID);
            if (result > 0)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                lblMessage.Text = MessageBox.MESSAGE_DELETE_SUCCESSFULLY;
                BindRole();
            }
            else
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                lblMessage.Text = MessageBox.MESSAGE_DELETE_NOT_SUCCESSFULLY;
            }
        }
        catch (Exception ex)
        {
        }
    }

    protected void grd_PageIndexChanged(object source, GridPageChangedEventArgs e)
    {
        BindRole();
    }

    private void BindAdminPage()
    {
        DataSet dsPage = new FunctionPageUI().GetChildAdminPage();
        if (TableLib.HasData(dsPage))
        {
            chkAdminPageList.DataSource = dsPage.Tables[0];
            chkAdminPageList.DataBind();
        }
    }

    private void ReInsertData()
    {
        panel1.Visible = true;
        lblRoleID.Text = "";
        txtRoleName.Text = "";
        txtRoleDescription.Text = "";
        btnSetRole.Text = "Thêm mới";
        BindAdminPage();
    }

    /// <summary>
    /// Hàm dùng để đánh dấu những trang thuộc quyền của người dùng đang sửa trên list các trang admin
    /// </summary>
    private void LoadPageInRole(DataTable dsRoleList)
    {
        foreach (DataRow row in dsRoleList.Rows)
        {
            foreach(ListItem item in chkAdminPageList.Items)
            {
                if (row["CategoryPageID"].ToString().Trim() == item.Value.Trim())
                {
                    item.Selected = true;
                    break;
                }
            }
        }
    }

    protected void btnAddRole_Click(object sender, EventArgs e)
    {
        ReInsertData();
    }

    private string GetCheckedPageList()
    {
        string list = "";
        foreach (ListItem item in chkAdminPageList.Items)
        {
            if (item.Selected)
            {
                list += item.Value.Trim() + ",";
            }
        }

        if (list != "") list = list.Substring(0, list.Length - 1);
        return list;
    }
}
